package org.krosa.net.http;

import static junit.framework.Assert.assertNotNull;

import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.krosa.net.http.entity.Response;
import org.krosa.net.http.impl.BasicHttpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Demo {
    final Logger logger = LoggerFactory.getLogger(Demo.class);

    /**
     * <p>
     * Sets up the unit tests.
     * </p>
     * 
     * @throws Exception
     *             to JUnit.
     */
    @Before
    public void setUp() throws Exception {
    }

    /**
     * <p>
     * Cleans up the unit tests.
     * </p>
     * 
     * @throws Exception
     *             to JUnit.
     */
    @After
    public void tearDown() throws Exception {
    }

    @Test
    public void testPlainHttpGet() throws Exception {
        Client c = new BasicHttpClient();
        String sample1 = "http://www.rd.xs.zj.cn/rd/main/A/index.html";
        Response r = c.get(sample1);
        assertNotNull(r);
        assertNotNull(r.getContent());
        logger.info(r.getCharSet());
        logger.info(r.getContent());
        logger.info("connect to server : " + r.getConnServerTime());
        logger.info("the server response time : " + r.getServerResponseTime());
        logger.info("the download speed : " + r.getDownSpeed() * 1000 + "B/S");
    }

    @Ignore
    @Test
    public void testHttpRedirect() throws Exception {
        Client c = new BasicHttpClient();
        String sample1 = "http://www.appstore.com";
        Response r = c.get(sample1);
        assertNotNull(r);
        assertNotNull(r.getContent());
        logger.info(r.getContent());
        logger.info("connect to server : " + r.getConnServerTime());
        logger.info("the server response time : " + r.getServerResponseTime());
        logger.info("the download speed : " + r.getDownSpeed() * 1000 + "B/S");
    }

    @Test
    public void testHttpsGet() throws Exception {
        Client c = new BasicHttpClient(true);
        String sample1 = "https://mail.dbappsecurity.com.cn:443/";
        Response r = c.get(sample1);
        assertNotNull(r);
        assertNotNull(r.getContent());
        logger.info(r.getContent());
        logger.info("connect to server : " + r.getConnServerTime() + " ms");
        logger.info("the server response time : " + r.getServerResponseTime() + " ms");
        logger.info("the download speed : " + r.getDownSpeed() * 1000 + " B/S");
    }

    @Test
    public void testHttpGetStress() throws Exception {
        String[] urls = new String[] { "http://www.baidu.com:80/",
                "http://www3.xinhuanet.com:80/", "http://www.shjcdj.cn:80/djw/", "http://www.jfdaily.com/",
                "http://www.zju.edu.cn:80/","http://www.bhphotovideo.com/","http://www.jiaxing.gov.cn:80/" };
        for(String url:urls) {
            Client c = new BasicHttpClient();
            Response r = c.get(url);
            assertNotNull(r);
            assertNotNull(r.getContent());
            logger.info("url is {},the content length is {} ",url,r.getContent().length());
        }
    }
}
